<?php 

function p($data, $varName = NULL)
{
    if(!is_null($varName))
      print_r($varName);
    echo "<pre>" . print_r($data, true) . "</pre>";
}

function arr_merge($arr)
{
    $re = array();

    foreach ($arr as $v) {
        $tmp = array_values($v);
        array_push($re, $tmp[0]);
    }

    return $re;
}

/**
 * 加密函数，用于密码等加密
 * @param  [type] $plaintext [description]
 * @return [type]            [description]
 */
function encode($plaintext)
{
  $ciphertext = $plaintext;
  return $ciphertext;
}

/**
 * 解密函数
 * @param  [type] $ciphertext [description]
 * @return [type]             [description]
 */
function decode($ciphertext)
{
  $plaintext = $ciphertext;
  return $plaintext;
}

/**
 * 读取文件
 * @param  [type] $path    [description]
 * @param  [type] $content [description]
 * @return [type]          [description]
 */
function readFormFile($path)
{
    if (file_exists($path)) {
        $handle = fopen($path, "r");//读取二进制文件时，需要将第二个参数设置成'rb'

        //通过filesize获得文件大小，将整个文件一下子读到一个字符串中
        $contents = fread($handle, filesize ($path));
        fclose($handle);
        return $contents;
    } else {
        return null;   
    }
}

/**
 * 写文件
 * @param  [type] $path    [description]
 * @param  [type] $content [description]
 * @return [type]          [description]
 */
function writeToFile($path, $content)
{
    $file_pointer = fopen($path,"w");       
    fwrite($file_pointer,$content);
    fclose($file_pointer);
}

/**
 * 截取字符串
 * @param  [type]  $str     [description]
 * @param  integer $start   [description]
 * @param  [type]  $length  [description]
 * @param  string  $charset [description]
 * @param  boolean $suffix  [description]
 * @return [type]           [description]
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
{
    if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset);
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix) return $slice."…";
    return $slice;
}

 ?>